package cn.newtouch.user.service.impl;

import cn.newtouch.basic.constant.RegisterConstant;
import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.service.impl.BaseServiceImpl;
import cn.newtouch.basic.util.Md5Utils;
import cn.newtouch.basic.util.StrUtils;
import cn.newtouch.org.domain.Shop;
import cn.newtouch.org.service.IShopService;
import cn.newtouch.user.domain.Logininfo;
import cn.newtouch.user.domain.User;
import cn.newtouch.user.dto.RegisterDto;
import cn.newtouch.user.mapper.LogininfoMapper;
import cn.newtouch.user.mapper.UserMapper;
import cn.newtouch.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

// @Transactional(readOnly = true,propagation = Propagation.SUPPORTS) 继承性
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;


    /**
     * 手机注册短信验证码
     * @param registerDto
     */
    @Override
    public void phoneRegister(RegisterDto registerDto) {

        String phone = registerDto.getPhone();
        String verifyCode = registerDto.getVerifyCode();
        String password = registerDto.getPassword();
        String comfirmPassword = registerDto.getComfirmPassword();

        //1.校验参数是否为空
        if(
                StringUtils.isEmpty(phone)||
                StringUtils.isEmpty(verifyCode)||
                StringUtils.isEmpty(password)||
                StringUtils.isEmpty(comfirmPassword)
        ){
            throw new BusinessException("参数不能为空！！");
        }
        // 2. 两次密码是否一致
        if(!password.equals(comfirmPassword)){
            throw new BusinessException("两次密码不一致，请重新输入！！");
        }
        // 3. 手机号校验，是否被注册
        User userPhone = userMapper.findByPhone(phone);
        if(userPhone!=null){
            throw new BusinessException("手机号已经被注册了，请直接登录！！");
        }
        // 4. 验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);
        if(obj==null){
            throw new BusinessException("验证码已过期，请重新获取！！");
        }
        // 5. 校验输入验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if(!redisCode.equalsIgnoreCase(verifyCode)){
            throw new BusinessException("验证码错误，请重新输入正确的验证码！！");
        }
        // 6. 保存数据 user  logininfo
        // 先去获取logininfo对象和user对象
        Logininfo logininfo = dto2Logininfo(registerDto);

        // 通过logininfo对象获取user对象
        User user = logininfo2User(logininfo);

        // 先保存logininfo表，返回自增的主键
        logininfoMapper.save(logininfo);

        // 把自增的主键赋值给user对象
        user.setLogininfo_id(logininfo.getId());
        // 保存user
        userMapper.save(user);
    }


    //写两个方法
    //获取User对象
    private User logininfo2User(Logininfo logininfo) {
        User user = new User();
        //工具类BeanUtils 复制属性
        BeanUtils.copyProperties(logininfo,user);

        return user;

    }


    //获取Logininfo对象
    public Logininfo dto2Logininfo(RegisterDto dto){

        //新建对象并且赋值
        Logininfo logininfo = new Logininfo();
        logininfo.setPhone(dto.getPhone());
        logininfo.setUsername(dto.getPhone());
        logininfo.setDisable(1);
        logininfo.setType(1);

        //密码和盐值处理
        //获取盐值（StrUtils工具类）
        String salt = StrUtils.getComplexRandomString(32);
        //加密（Md5Utils工具类）  加盐加密
        String dbPassword = Md5Utils.encrypByMd5(salt + dto.getPassword());

        //赋值给对象
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);

        //返回对象
        return logininfo;
    }

}
